<template>
	<view class="page">
		<!-- <view class="list">
			<view class="list__left">表单分类</view>
			<view class="list__right">
				<u-icon name="arrow-right" size="25"></u-icon>
			</view>
		</view> -->
		<view class="category">
			<view class="category__head">表单分类</view>
			<view class="category__content">
				<c-radio-group
				    v-model="radioValue"
				    @change="radioChange"
				    placement="column"
				>
				    <c-radio
				        v-for="(radioItem, radioIndex) in category"
				        :key="radioIndex"
				        :name="radioItem.TypeName"
				    >
				    </c-radio>
				</c-radio-group>
			</view>
		</view>
		
		<view class="menu">
			<view class="menu__btn">
				<u-button type="primary" color="#faad14" text="存草稿" @click="save"></u-button>
			</view>
			<view class="menu__btn">
				<u-button type="primary" color="#2979ff" text="发布" @click="confirm"></u-button>
			</view>
		</view>
	</view>
</template>

<script>
	import _custom from '@/common/_custom.js'
	import _data from '@/common/_data.js'
	import cRadioGroup from '@/components/custom/c-radio-group/c-radio-group.vue';
	import cRadio from '@/components/custom/c-radio/c-radio.vue';
	import {sendBackFunction} from '@/common/custom_util.js'
	
	export default {
		components: {
			cRadioGroup,
			cRadio
		},
		data() {
			return {
				obsusername: _data.localData('name'),
				pxid: _data.localData('userid'),
				data: {},
				form: {},
				category: [],
				radioValue: {
					value: '',
					label: '未分类'
				}
			}
		},
		onLoad(options) {
			try {
				options.data ? this.data = JSON.parse(options.data) : '';
				this.form = options.form;
				this.getSmartFormType();
			} catch(e) {}
		},
		methods: {
			/**
			 * 获取表单模板类型
			 */
			getSmartFormType() {
				let that = this;
				_custom.getDataJava({
					apiname: 'getSmartFormType',
					success(res) {
						res.push({
							TypeName: '未分类'
						})
						that.category = res;
					},
				})
			},
			/**
			 * 表单创建
			 * @param {Object} v
			 */
			addSmartForm(v) {
				let that = this;
				_custom.getDataJava({
					apiname: 'addSmartForm',
					code: 3,
					apiparam: v,
					success() {
						uni.showToast({
							title: "创建成功",
							icon: 'none'
						})
						sendBackFunction('leftMenuInit',null,2);
					},
					fail() {
						uni.showToast({
							title: "创建失败",
							icon: 'none'
						})
					}
				})
			},
			/**
			 * 修改动态表单(仅限草稿)
			 * @param {Object} v
			 */
			updateSmartForm(v) {
				let that = this;
				_custom.getDataJava({
					apiname: 'updateSmartForm',
					code: 2,
					apiparam: v,
					success() {
						uni.showToast({
							title: "操作成功",
							icon: 'none'
						})
						sendBackFunction('leftMenuInit',null,2);
					},
					fail() {
						uni.showToast({
							title: "操作失败",
							icon: 'none'
						})
					}
				})
			},
			radioChange(e) {
				this.radioValue.label = e;
			},
			save() {
				let form = this.form.replace(/\[\{/g,"[[{");
				form = form.replace(/}]/g,"}]]");
				let data = {};
				if(this.data && this.data.id) {
					data = {
						"formid": this.data.id,
						"formtitle": this.data.title,
						"formdesc": this.data.des,
						"coverimage": this.data.cover.path,
						"typename": this.radioValue.label,
						"isdraft": "1", //是否存草稿 0：直接发布，1：存草稿箱
						"formjson": form ,//模板配置Json
						"createuser": this.obsusername,
						"createpxid": this.pxid
					}
					this.updateSmartForm(data)
				} else {
					data = {
						"createuser": this.obsusername,
						"createpxid": this.pxid,
						"formtitle": this.data.title,
						"formdesc": this.data.des,
						"coverimage": this.data.cover.path,
						"typename": this.radioValue.label,
						"isdraft": "1", //是否存草稿 0：直接发布，1：存草稿箱
						"formjson": form ,//模板配置Json
						"createuser": this.obsusername,
						"createpxid": this.pxid
					}
					this.addSmartForm(data);
				}
			},
			confirm() {
				let form = this.form.replace(/\[\{/g,"[[{");
				form = form.replace(/}]/g,"}]]");
				let data = {}
				if(this.data && this.data.id) {
					data = {
						"formid": this.data.id,
						"formtitle": this.data.title,
						"formdesc": this.data.des,
						"coverimage": this.data.cover.path,
						"typename": this.radioValue.label,
						"isdraft": "0", //是否存草稿 0：直接发布，1：存草稿箱
						"formjson": form ,//模板配置Json
						"createuser": this.obsusername,
						"createpxid": this.pxid
					}
					this.updateSmartForm(data)
				} else {
					data = {
						"createuser": this.obsusername,
						"createpxid": this.pxid,
						"formtitle": this.data.title,
						"formdesc": this.data.des,
						"coverimage": this.data.cover.path,
						"typename": this.radioValue.label,
						"isdraft": "0", //是否存草稿 0：直接发布，1：存草稿箱
						"formjson": form ,//模板配置Json
						"createuser": this.obsusername,
						"createpxid": this.pxid
					}
					this.addSmartForm(data);
				}
			}
		}
	}
</script>

<style scoped lang="scss">
	.list {
		height: 100rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 0 20rpx;
		background-color: #fff;
	}
	
	.category {
		padding: 20rpx;
		background-color: #fff;
		
		&__head {
			font-size: 33rpx;
		}
	}
	
	.menu {
		height: 100rpx;
		background-color: #fff;
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		padding: 20rpx;
		display: flex;
		justify-content: space-between;
		box-shadow: 0 -1rpx 20rpx 1rpx rgba(0, 0, 0, .1);
		
		&__btn {
			width: 340rpx;
		}
	}
</style>
